package entities;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import utility.vec2;
import android.os.SystemClock;

import com.liquidcode.cellular.Texture;

public class CSprite {
	protected ByteBuffer vbb;
	protected FloatBuffer vertexBuffer;
	protected ShortBuffer indexBuffer;
	protected FloatBuffer mTextureBuffer;
	protected int mTextureID = -1;
	protected float scale;
	protected float width;
	protected float height;
	protected boolean m_bCentered = true;

	protected float vertices[] = { 
			-1.0f, 1.0f, 0.0f, // 0, top Left (0,1,2)
			1.0f, 1.0f, 0.0f, // 1, top right (3,4,5)
			-1.0f, -1.0f, 0.0f, // 2, bottom left (6,7,8)
			1.0f, -1.0f, 0.0f, // 3, bottom Right (9,10,11)
	};

	protected short[] indices = { 0, 1, 2, 1, 2, 3 };

	protected float textureCoordinates[] = { 
			0.0f, 1.0f, // top left
			1.0f, 1.0f, // top right
			0.0f, 0.0f, // bottom left
			1.0f, 0.0f, // bottom right
	};

	// Default Constructor
	public CSprite() {
		// set up basic vert data
		// a float is 4 bytes, therefore we multiply the number of vertices with
		// 4.
		vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		vertexBuffer = vbb.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);

		// set index data
		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
		ibb.order(ByteOrder.nativeOrder());
		indexBuffer = ibb.asShortBuffer();
		indexBuffer.put(indices);
		indexBuffer.position(0);

		// set texture data
		ByteBuffer byteBuf = ByteBuffer
				.allocateDirect(textureCoordinates.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		mTextureBuffer = byteBuf.asFloatBuffer();
		mTextureBuffer.put(textureCoordinates);
		mTextureBuffer.position(0);
		m_bCentered = true;
		mTextureID = -1;
	}

	/*****************************
	 * Class Methods
	 ******************************/
	public void Update(float time) {
		
	}

	public void Shutdown() {
		// TODO:
		// If we need to explicitly clean up any memory
		// we can call finalize in here as a sanity check.
		vertexBuffer.clear();
		indexBuffer.clear();
		mTextureBuffer.clear();
	}

	/****************************
	 * Accessors
	 *****************************/
	public float getScale() {
		return scale;
	}

	public float[] getVerts() {
		return vertices;
	}

	public FloatBuffer getVB() {
		return vertexBuffer;
	}

	public ShortBuffer getIB() {
		return indexBuffer;
	}

	public FloatBuffer getTexBuffer() {
		return mTextureBuffer;
	}

	public int getNumVerts() {
		return vertices.length;
	}

	public int getNumIndices() {
		return indices.length;
	}

	public int getTextureID() {
		return mTextureID;
	}

	public float getWidth() {
		return width;
	}

	public float getHeight() {
		return height;
	}

	/****************************
	 * Mutators
	 *****************************/
	public void setScale(float s) {
		scale = s;
	}

	public void setTexID(int id) {
		
		mTextureID = id;
	}
	
	public void setTexture( Texture t, boolean centered )
	{
		if( t != null )
		{
			m_bCentered = centered;
			mTextureID = t.id;
			setWidth(t.width);
			setHeight(t.height);
			SetUpQuad(t.width, t.height);
	
		}
	}
	public void SetUpQuad(float w, float h)
	{
		if( m_bCentered )
		{
			// top left
			vertices[0] = -w*0.5f; // bottom left x
			vertices[1] = -h*0.5f; // bottom left y
			vertices[2] = 0.0f; // bottom left z
			
			// top left
			vertices[3] = w*0.5f; // top left x
			vertices[4] = -h*0.5f; // top left y
			vertices[5] = 0.0f; // top left z
			
			// bottom left
			vertices[6] = -w*0.5f; // bottom right x
			vertices[7] = h*0.5f;
			vertices[8] = 0.0f; // bottom right z
	
			// bottom right
			vertices[9] = w*0.5f; // top right x
			vertices[10] = h*0.5f; // top right y
			vertices[11] = 0.0f; // top right z
		}
		else
		{
			// top left
			vertices[0] = 0.0f; // bottom left x
			vertices[1] = 0.0f; // bottom left y
			vertices[2] = 0.0f; // bottom left z
						
			// top right
			vertices[3] = w; // top left x
			vertices[4] = 0.0f; // top left y
			vertices[5] = 0.0f; // top left z
					
			// bottom left
			vertices[6] = 0.0f; // bottom right x
			vertices[7] = h;
			vertices[8] = 0.0f; // bottom right z
			
			// bottom right
			vertices[9] = w; // top right x
			vertices[10] = h; // top right y
			vertices[11] = 0.0f; // top right z
		}
		


		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
	}

	public void setWidth(float w) {
		width = w;
	}

	public void setHeight(float h) {
		height = h;
	}
}
